#coding=utf-8

import types
import commonHelper


# 获得列名的索引
def getColNameDic(titleNames):
    dic = {}
    index = 0
    for i , name in enumerate(titleNames.split(",")):
        name = commonHelper.parseProperites(name)
        dic[name] = index
        index = index + 1
    return dic


# 处理一个文件 ,格式
def dealOneFile(fileReader,colKeyData,colKeyArray,titleTypes,titleNames,colNameDic):
    # 这个配置数据
    confData = {}
    numIndex = 0 #每一条记录的id
    while True:
        line = commonHelper.fileReadLine(fileReader)  #这一行配置
        if line:
            lineInfo = commonHelper.splitConfLine(line)  # 这一行数据的分割
            if not "data" in confData.keys():
                confData["data"] = {}
            if lineInfo[0]:
                confData["data"][numIndex] = genOneConf(lineInfo, titleNames, titleTypes)  # 原本的数据记录
                addDicData(confData, lineInfo, numIndex, colKeyData, colNameDic)
                addDicDataArray(confData, lineInfo, numIndex, colKeyArray, colNameDic)
        else :
            break
        numIndex = numIndex + 1
    return confData

#生成一条配置 目前转为数组，类型已转换好
def genOneConf(lineData,titleNames,titleTypes):
    arr = []
    for (i, titleConf) in enumerate(titleNames):
        if titleConf:
            titleType = titleTypes[i]
            if titleType == "int" or titleType == "long" or titleType == "short" or titleType == "smallint":
                try:
                    arr.append(int(lineData[i]))
                except:
                    arr.append(float(lineData[i]))
            elif titleType == "float" or titleType == "double" or titleType == "number":
                arr.append(float(lineData[i]))
            else:
                arr.append(str(lineData[i]))
    return arr

    # obj = {}
    # for (i,titleConf) in enumerate(titleNames):
    #     if titleConf:
    #         titleType = titleTypes[i]
    #         if titleType == "int" or titleType == "long" or titleType == "short" or titleType == "smallint" :
    #             try:
    #                 obj[titleConf] = int(lineData[i])
    #             except :
    #                 obj[titleConf] = float(lineData[i])
    #         elif  titleType == "float" or titleType == "double" or  titleType == "number":
    #             obj[titleConf] = float(lineData[i])
    #         else :
    #             obj[titleConf] = str(lineData[i])
    # return obj


def converTitle(titleNames):
    for (i, titleConf) in enumerate(titleNames):
        # if(len(titleConf) > 5):
        #     titleConf = titleConf[0:5]
        titleNames[i] = commonHelper.parseProperites(titleConf)
    return titleNames

def converKeys(keysList):
    for (i, keys) in enumerate(keysList):
        for (j, key) in enumerate(keys):
            keys[j] = commonHelper.parseProperites(key)
    return keysList


#添加词典数据
def addDicData(confData,lineData,lineDataIndex,colKeyData,colNameDic):
    for (i,keyData) in enumerate(colKeyData):
        dicName = commonHelper.getDicNameByKeys(keyData,False)
        key = ""
        # 词典名字
        for (j, colName) in enumerate(keyData):
            if key != "":
                key += "_"
            key += lineData[colNameDic[colName]]
        if not dicName in confData.keys():
            confData[dicName] = {}
        confData[dicName][key] = lineDataIndex

#添加词典数据数组
def addDicDataArray(confData,lineData,lineDataIndex,colKeyData,colNameDic):
    for (i,keyData) in enumerate(colKeyData):
        dicName = commonHelper.getDicNameByKeys(keyData,True)
        key = ""
        # 词典名字
        for (j, colName) in enumerate(keyData):
            if key != "":
                key += "_"
            key += lineData[colNameDic[colName]]
        if not dicName in confData.keys():
            confData[dicName] = {}
        if not key in confData[dicName]:
            confData[dicName][key] = []
        confData[dicName][key].append(lineDataIndex)